﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Util;

using BrightIdeasSoftware;

namespace FCNS.KeepMoney.Forms
{
    public partial class InterestForm : FCNS.KeepMoney.Forms.ParentForm
    {
        public InterestForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        List<InterestField> interestField = null;
        ComboBox comboBox = new ComboBox();

        public void Init()
        {
            FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1, Properties.Resources.interest_32,
                "配置利息模式", "在这里配置 定期账务 使用的利息模式");

            HotItemStyleUtil.SetHotItemStyle(listViewEntry, false);
            listViewEntry.AddDecoration(new EditingCellBorderDecoration(true));

            labelError.Text = string.Empty;
            interestField = InterestField.GetInterestField;

            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox.Items.AddRange(Enum.GetNames(typeof(InterestField.InterestMode)));

            listViewEntry.SetObjects(interestField);
        }

        private bool HaveUsedTheRecording(InterestField field)
        {
            return (RegularEntryField.GetRegularEntryField.Find(ef =>
            {
                return ef.InterestId == field.Id;
            }) != null);
        }


        private void dataListView1_CellEditStarting(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            InterestField field = (InterestField)listViewEntry.SelectedObject;
            if (HaveUsedTheRecording(field))
            {
                labelError.Text = Util.LanguageUtil.此行已有使用记录_无法编辑;
                e.Cancel = true;
                return;
            }

            if (e.Column.AspectName != "columnMode")
                return;

            comboBox.Location = e.Control.Location;
            comboBox.Size = e.Control.Size;
            comboBox.SelectedIndex = 0;
            e.Control = comboBox;
        }

        private void dataListView1_CellEditFinishing(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            if (e.Value == e.NewValue)
                return;

            InterestField field = (InterestField)listViewEntry.SelectedObject;
            switch (e.Column.AspectName)
            {
                case "columnName":
                    field.columnName = e.NewValue.ToString();
                    break;

                case "columnMode":
                    if (comboBox.SelectedIndex == field.Mode)
                        return;

                    field.Mode = comboBox.SelectedIndex;
                    e.NewValue = comboBox.Text;
                    break;

                case "columnValue":
                    decimal de = 0;
                    decimal.TryParse(e.NewValue.ToString(),System.Globalization.NumberStyles.Any,null,out de);
                    if (de == field.Value||de==0)
                        return;

                    field.Value = de;
                    break;
            }

            listViewEntry.RefreshObject(listViewEntry.SelectedObject);
            Program.SaveKMD = true;
        }

        private void dataListView1_SelectionChanged(object sender, EventArgs e)
        {
            labelError.Text = string.Empty;
        }


        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listViewEntry.SelectedObject == null)
                return;

            InterestField field=(InterestField)listViewEntry.SelectedObject;
            if (HaveUsedTheRecording(field))
                labelError.Text=Util.LanguageUtil.此行已有使用记录_无法删除;
            else
            {
                field.Remove();
                listViewEntry.SetObjects(interestField);
                Program.SaveKMD = true;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            InterestField field = new InterestField(null);
            field.AddRowToDataTable();
            listViewEntry.SetObjects(interestField);
            Program.SaveKMD = true;
        }
    }
}
